﻿using System.Runtime.InteropServices;

namespace Lesson12_练习题;

#region 练习题1
//一家三口 妈妈做饭，爸爸和儿子都要 吃饭
//用委托来模拟 做饭 开饭 吃饭 过程
abstract class Person
{
    public abstract void Eat();
}

class Mother: Person
{
    public Action action;
    public override void Eat()
    {
        Console.WriteLine("妈妈吃饭");
    }

    public void Cook()
    {
        Console.WriteLine("妈妈做饭");
        Console.WriteLine("饭做好了");
        action();
    }
}

class Father : Person
{
    public override void Eat()
    {
        Console.WriteLine("爸爸吃饭");
    }
}
class Son : Person
{
    public override void Eat()
    {
        Console.WriteLine("儿子吃饭");
    }
}

#region 练习题二
//怪物死亡后，要增加10金币，并且更新界面
//成就要累加击杀怪物数，用委托来实现这些功能
//只要写核心逻辑就可以，不用写太复杂

class Monster
{
    public int money = 10;  //怪物为10金币
    
    //委托作为类成员使用
    public Action<Monster>  deadDoSomething;   //过初始化并赋值给这个委托，你可以让 Monster 对象在死亡时执行一系列预定义的操作

    public void Dead()
    {
        if (deadDoSomething != null)
        {
            deadDoSomething(this);    //托允许将方法作为参数进行传递 
        }
        //一般情况下委托关联的函数有加就有减
        deadDoSomething = null;  //这个怪物一次死亡的逻辑实现
        //怪物死亡逻辑已经实现，无需重复调用
    }
}

class Player:Monster
{
    public int myMoney = 0;

    public void MonsterdeadDoSomething(Monster monster)  //该方法接收一个 Monster 类型的参数这表明它是为了处理特定怪物实例的死亡事件而设计的。
    {
        myMoney += monster.money;
        Console.WriteLine("玩家现在有{0}金币", myMoney);
    }

}

class UpDateUI:Monster
{
   public int  NowMoney = 0;
   public void MonsterdeadDoSomething(Monster monster)  //这表明它是为了处理特定怪物实例的死亡事件而设计的。
   {
       NowMoney += monster.money;
       Console.WriteLine("当前界面有{0}金币", NowMoney);
   }
}

class Accomplishment:Monster
{
    public int NowKillMonsterCount ;

    public void KillMonster(Monster monster)
    {
        NowKillMonsterCount += 1 ;
        Console.WriteLine("当前击杀怪物数量为{0}", NowKillMonsterCount);
    }
}

#endregion


#endregion
class Program
{
    static void Main(string[] args)
    {
        Mother m = new Mother();
        Father f = new Father();
        Son s = new Son();
        m.action += f.Eat;
        m.action += s.Eat;
        m.action += m.Eat;
        m.Cook();
        
        Monster monster= new Monster();  //实例化一个Monster对象
        Player p = new Player();
        UpDateUI u = new UpDateUI();
        Accomplishment ac = new Accomplishment();
        
        monster.deadDoSomething += p. MonsterdeadDoSomething;
        monster.deadDoSomething += u.  MonsterdeadDoSomething;
        monster.deadDoSomething += ac.KillMonster;

        monster.Dead();
        
        Monster monster1= new Monster();  //实例化一个Monster对象
        // Player p1 = new Player();//由于每个Player、UpDateUI实例中的金币或击杀数量都是独立的，所以对monster1的操作不会影响到之前创建的那些对象（如p、u、ac）所持有的金币或击杀数状态
        // UpDateUI u1 = new UpDateUI();
        // Accomplishment ac1 = new Accomplishment();
        
        monster1.deadDoSomething += p. MonsterdeadDoSomething;
        monster1.deadDoSomething += u.  MonsterdeadDoSomething;
        monster1.deadDoSomething += ac.KillMonster;

        monster1.Dead();



    }
}